package com.qls.ali.biz.order.service.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qls.ali.biz.order.service.biz.OrderNotice;
import com.qls.ali.biz.order.service.biz.ReceOrderService;
import com.qls.ali.biz.order.service.mybatis.DtStoreService;
import com.qls.ali.biz.order.util.OrderConstant;
import com.qls.core.entity.DtStore;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author doyoung
 */
@Slf4j
@Service
public class OrderNoticeImpl implements OrderNotice {

    @Autowired
    private DtStoreService dtStoreService;

    @Autowired
    private ReceOrderService receOrderService;

    @Override
    public String router(String xml) {
        String result = "";
        Document document = null;
        try {
            document = DocumentHelper.parseText(xml);
        } catch (DocumentException ignored) {
        }
        if (Objects.isNull(document)) {
            return result;
        }
        Element root = document.getRootElement();
        String rootName = root.getName();
        Element authNode = root.element("AuthenticationToken");
        String username = authNode.element("Username").getStringValue();
        String password = authNode.element("Password").getStringValue();

        //权限验证
        DtStore dtStoreEntity = dtStoreService.getBaseMapper().selectOne(new QueryWrapper<DtStore>().eq("ali_cb_user", username).eq("ali_cb_user_pass", password));
        if (dtStoreEntity == null) {
            log.warn("username:{},权限验证失败", username);
            result = "权限验证失败";
            return result;
        }
        Integer dtId = dtStoreEntity.getDtId();
        Integer storeId = dtStoreEntity.getId();
        OrderConstant.AliOrderRouter aliOrderRouter = OrderConstant.AliOrderRouter.valueOf(rootName);
        switch (aliOrderRouter) {
            // 验单（阿里价格库存验证，携程检验是否可预定）
            case ValidateRQ:
                log.info("username：{}，ali验单通知xml：{}", username, xml);
                result = receOrderService.validate(xml);
                log.info("username：{}，ali验单返回xml：{}", username, result);
                break;
            // 预定
            case BookRQ:
                log.info("username：{}，ali预定通知xml：{}", username, xml);
                result = receOrderService.book(xml, dtId, storeId);
                log.info("username：{}，ali预定返回xml：{}", username, result);
                break;
            // 支付成功通知
            case PaySuccessRQ:
                log.info("username：{}，ali支付成功通知xml：{}", username, xml);
                result = receOrderService.paySuccess(xml, username);
                log.info("username：{}，ali支付成功返回xml：{}", username, result);
                break;
            // 查询订单
            case QueryStatusRQ:
                log.info("username：{}，ali查询订单通知xml：{}", username, xml);
                result = receOrderService.queryStatus(xml);
                log.info("username：{}，ali查询订单返回xml：{}", username, result);
                break;
            // 退款通知
            case OrderRefundRQ:
                log.info("username：{}，ali退款通知xml：{}", username, xml);
                result = receOrderService.orderRefund(xml);
                log.info("username：{}，ali退款返回xml：{}", username, result);
                break;
            // 取消订单通知
            case CancelRQ:
                log.info("username：{}，ali取消订单通知xml：{}", username, xml);
                result = receOrderService.cancel(xml);
                log.info("username：{}，ali取消订单返回xml：{}", username, result);
                break;
            default:
                break;
        }
        return result;
    }
}
